﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DTO;
namespace CungMuaC5.Products
{
    public partial class ThanhToan_2 : System.Web.UI.Page
    {
        BUS_KhuyenMai PickedProductDetail = new BUS_KhuyenMai();
        public DTO_NguoiNhan receiver = new DTO_NguoiNhan();

        protected void Page_Load(object sender, EventArgs e)
        {
            div1.Attributes.Add("onclick", "radiochoose(document.getElementById('" + RadioButtonHT1.ClientID + "'))");
            div2.Attributes.Add("onclick", "radiochoose(document.getElementById('" + RadioButtonHT2.ClientID + "'))");
            div3.Attributes.Add("onclick", "radiochoose(document.getElementById('" + RadioButtonHT3.ClientID + "'))"); 
            DropDownList soluong;

            if (!IsPostBack)
            {
                List<DTO_KhuyenMai> detail = PickedProductDetail.GetDetails(Request.QueryString["MaKM"]);
                Label dongia = (Label)BuyProductTable1.FindControl("lblDonGia");
                dongia.Text = detail[0].DonGiaMoi.ToString();
                Label thanhtien = (Label)BuyProductTable1.FindControl("lblThanhTien");
                soluong = (DropDownList)BuyProductTable1.FindControl("SoLuong");

                DTO_DonHang dh = new DTO_DonHang();
                dh = Session.GetDonHang();
                if(Request.UrlReferrer.ToString().EndsWith("ThanhToan_1.aspx?makm="+Request.QueryString["MaKM"]))
                {
                    soluong.SelectedIndex = dh.SoLuong;
                }
                if (Request.UrlReferrer.ToString().EndsWith("ThanhToan_2.aspx?makm=" + Request.QueryString["MaKM"]) || Request.UrlReferrer.ToString().EndsWith("ThanhToan_3.aspx?makm=" + Request.QueryString["MaKM"]))
                {
                    soluong.SelectedIndex = dh.SoLuong;
                    if (dh.HinhThucThanhToan == 0)
                        RadioButtonHT1.Checked = true;
                    if (dh.HinhThucThanhToan == 1)
                        RadioButtonHT2.Checked = true;
                    if (dh.HinhThucThanhToan == 2)
                        RadioButtonHT3.Checked = true;
                }

                thanhtien.Text = (Convert.ToInt32(dongia.Text) * Convert.ToInt32(soluong.SelectedValue.ToString())).ToString() + " đ";
                Label tong = (Label)BuyProductTable1.FindControl("lblTong");
                tong.Text = thanhtien.Text;
                Label name = (Label)BuyProductTable1.FindControl("lblName");
                name.Text = detail[0].TenKM;
            }

            DetailsView2.DataSource = PickedProductDetail.GetDetails(Request.QueryString["MaKM"]);
            DetailsView2.DataBind();
        }

        protected void ImageButtonContinue_Click(object sender, ImageClickEventArgs e)
        {
            DTO_DonHang dh = new DTO_DonHang();
            dh = Session.GetDonHang();
            if (RadioButtonHT1.Checked)
                dh.HinhThucThanhToan = 0;
            if (RadioButtonHT2.Checked)
                dh.HinhThucThanhToan = 1;
            if (RadioButtonHT3.Checked)
                dh.HinhThucThanhToan = 2;
            DropDownList soluong = (DropDownList)BuyProductTable1.FindControl("SoLuong");
            dh.SoLuong = soluong.SelectedIndex;
            Session.SetDonHang(dh);
            Response.Redirect("~/Products/ThanhToan_3.aspx?makm=" + Request.QueryString["MaKM"]);
        }
    }
}